import $axios from "@/common/http.js";//请求实例
import { Message } from "element-ui";
import router from "@/router"

/**
 * 
 * @returns  分页总页数
 */
export const contotal = async () => {
    let { list } = await $axios.get('/usercount')
    return list
}

/**
 * 
 * @param {*} param0  管理员分页
 * @returns 
 */
export const getUser = async ({ page = 1, size = 2 }) => {
    // console.log(id);
    return await $axios.get('/userlist', { params: { page, size } })
}

/**
 * 
 * @param {*} data 管理员添加
 */

export const getUserAdd = async (data) => {
    let rst = await $axios.post('/useradd', { ...data })
    if (rst.code == 200) {
        Message({
            type: 'success',
            message: rst.msg,
            duration: 500,
            onClose() {
                router.back()
            }
        })
    }
}


/**
 * 
 * @param {*} id 根据id 查找一条数据  管理员
 * @returns 
 */
export const getUserinof = async (id) => {
    return await $axios.get('/userinfo', { params: { uid: id } })
}


/**
 * 
 * @param {*} data 修改管理员
 */
export const setUser = async (data) => {
    let rst = await $axios.post('/useredit', { ...data })
    if (rst.code == 200) {
        Message({
            type: 'success',
            message: rst.msg,
            duration: 500,
            onClose() {
                router.back()
            }
        })
    }
}

/** 
 *  管理员删除
 * @param {*} id 
 * @returns 
 */

export const delUser = async (id) => {
    let rst = await $axios.post('/userdelete', { id })
    if (rst.code == 200) {
        Message({
            type: 'success',
            message: rst.msg,
            duration: 500,
        })
        return true
    }
    return false
}